#! /usr/bin/python

# Author: William G.K. Martin (wgm2111@cu where cu=columbia.edu)
# copyright (c) 2010
# liscence: BSD style

# python imports 
from __future__ import print_function
from sys import argv


# Templates 
class PyCode:
    " Create an object that stores a python code template " 
    
    # The header for all my files
    header = """
#
# Author: William G.K. Martin (wgm2111@cu where cu=columbia.edu)
# copyright (c) 2010
# liscence: BSD style
#
# ================================================================================

"""
    # python standard import statements
    standard_imports = """
# standard python imports
from __future__ import print_function
"""
    # other import statement section
    other_imports = """
# third party imports 
from scipy import 
from numpy import 
from numexpr import 
from tables import
"""
    plot_imports = """from matplotlib import
from enthought.mayavi.mlab import
"""
    test_imports = """import nose.tools as nt
import numpy.testing as npt
"""


    # my imports 
    my_imports = """
# my imports 

"""

    # global definitions 
    global_definitions = """
# Global variables
# ================================================================================


"""
    # function and class definitions 
    definitions = """
# function and class definitions 
# ================================================================================


"""
    # script definition at the end of the file
    end_script = """
# ====
# Example script
# ================================================================================

if __name__=="__main__":

    # imports

    #script
    pass

"""

    def __init__(self, name):
        " Construct a PyCode object by giving the name of the project "
        self.name = name
        self.modname_import = 'import ' + str(name)
        self.module_imports = self.other_imports + "\n"
        self.test_other_imports = self.other_imports + self.test_imports
        self.test_my_imports = self.my_imports + self.modname_import
        self.example_other_imports = self.other_imports + self.plot_imports
        self.example_my_imports = self.my_imports + self.modname_import


    def module_pattern(self):
        " Print a module format python program "
        out = ""
        names = ['header', 'standard_imports', 'other_imports', 'my_imports', 
                 'global_definitions','definitions' ,'end_script']
        for name in names:
            out += getattr(self, name)
        return out
    
    def example_pattern(self):
        " example code pattern generator "
        out = ""
        names = ['header', 'standard_imports', 'example_other_imports', 
                 'example_my_imports', 'global_definitions','definitions',
                 'end_script']
        for name in names:
            out += getattr(self, name)
        return out
        
    def test_pattern(self):
        " Test code pattern generator "
        out = ""
        names = ['header', 'standard_imports', 'test_other_imports', 
                 'test_my_imports', 'global_definitions','definitions',
                 'end_script']
        for name in names:
            out += getattr(self, name)
        return out
    
# Script to make the file
if __name__=="__main__":
    

    # Check that the input is a name and that the file doesn't exist already
    if len(argv) == 2:
        name = str(argv[1])
    else:
        raise ValueError("Expected exactly one input for the name of module")
    try: 
        open(module_name, 'r')
        raise ValueError('File already exists. Delete manually to continue.')
    except: pass

    # instantiate PyCode object with given name
    pycode = PyCode(name)

    # write module to file 
    module_name = name+".py"
    module = open(module_name, 'w')
    module.write(pycode.module_pattern())
    module.close()
    
    # write module to file 
    example_name = name+"_example.py"
    example = open(example_name, 'w')
    example.write(pycode.example_pattern())
    example.close()

    # write module to file 
    test_name = name+"_test.py"    
    test = open(test_name, 'w')
    test.write(pycode.test_pattern())
    test.close()
